package com.example.lib_login.phone

import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.animation.addListener
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.example.lib_login.LoginViewModel
import com.example.lib_login.dataBinding.BindAttribute
import com.example.lib_login.dataBinding.Events
import com.example.lib_login.databinding.WjbBinding


class PhoneFragment : Fragment(){
    private lateinit var binding:WjbBinding
    private var isFirst = true
    private val viewModel:LoginViewModel by activityViewModels()
    private lateinit var mContext:Context


    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        binding = WjbBinding.inflate(inflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //初始化布局文件中的data
        binding.event = Events()
        binding.viewModel = viewModel
        binding.bindAttr = BindAttribute





        val rootView = requireActivity().window.decorView.rootView

        //用于监听视图层次结构布局发生变化的事件
        rootView.viewTreeObserver.addOnGlobalLayoutListener {

            //布局发生变化时的处理逻辑
            val r = Rect()
            //用于获取窗口的可见显示区域的方法
            rootView.getWindowVisibleDisplayFrame(r)
            val viewHeight = rootView.height
            //获取View可见区域的bottom
            val visibleHeight = r.bottom

            if (visibleHeight != viewHeight) {
                isFirst = false
                if(viewModel.displaySwitch) {
                    //键盘显示

                    startAnim1()
                }else{
                    binding.constraint.top = -43
                }
                viewModel.hiddenSwitch = true
            } else {
                //键盘隐藏
                if(viewModel.hiddenSwitch) {
                    if (!isFirst) {

                        startAnim2()
                    }
                }
                viewModel.displaySwitch = true
            }
        }


        binding.manager = requireActivity().supportFragmentManager



    }

    override fun onDetach() {
        super.onDetach()
        Log.v("wjb","${context==null}")
    }



    private fun startAnim1(){
        //启动键盘显示时，布局动画
        val anim1 = ValueAnimator.ofInt(150,-43).apply {
            addUpdateListener {
                binding.constraint.top = dp2px((it.animatedValue) as Int)

            }

        }

        val anim2 = ValueAnimator.ofFloat(1f,0f).apply {
            addUpdateListener {
                binding.logoImageView.alpha = it.animatedValue as Float
            }

        }

        val anim3  = ValueAnimator.ofFloat(0f,1f).apply {

            addUpdateListener {
                binding.smallView.alpha = it.animatedValue as Float
                binding.smallText.alpha = it.animatedValue as Float
            }

            addListener(
                onStart = {

                }
            )

        }
        AnimatorSet().apply {
            duration = 600
            playTogether(anim1,anim2,anim3)
            start()
        }

    }





    private fun startAnim2(){
        //键盘隐藏
        val anim1 = ValueAnimator.ofInt(-43, 150).apply {

            addUpdateListener {
                binding.constraint.top = dp2px((it.animatedValue) as Int)
            }

        }

        val anim2 = ValueAnimator.ofFloat(0f,1f).apply {

            addUpdateListener {
                binding.logoImageView.alpha = it.animatedValue as Float
            }

            addListener(
                onStart = {

                }
            )

        }
        val anim3  = ValueAnimator.ofFloat(1f,0f).apply {

            addUpdateListener {
                binding.smallView.alpha = it.animatedValue as Float
                binding.smallText.alpha = it.animatedValue as Float
            }

            addListener(

            )

        }

        AnimatorSet().apply {
            duration = 600
            playTogether(anim1,anim2,anim3)
            start()
        }

    }





    fun dp2px(dp:Int) :Int {
        return (mContext.resources.displayMetrics.density * dp).toInt()
    }



}


